<template>
	<view class="content">
		<van-nav-bar title="请登录" left-arrow @click-left="onClickLeft" />
		<view class="profilePhoto">
			<image class="imgLogo" src="../../static/img/logo.png" mode=""></image>
		</view>
		<view class="">
			<text class="hint">申请获取你微信绑定的手机号</text>
		</view>
		<view class="text-area">
			<button class="btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">手机号一键登录</button>
		</view>
		<view class="zhuanBox">
			<text class="txt">登录/注册即表示您已阅读、理解并同意</text>
			<navigator class="link" url="../myPage/regards/privacy/privacy">《诸安科技隐私政策》</navigator>
		</view>

	</view>
</template>

<script>
	import request from '../../request/request.js'
	import {
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				title: 'Hello',
				mobile: '诸安科技',
			}
		},
		onLoad: function() {
			this.$store.commit('checksession')
		},
		methods: {
			...mapMutations(['changisLogin']), //==function changisLogin () {}
			//获取用户手机号授权，进行登录。
			onGetPhoneNumber(e) {
				let encryptedData = e.detail.encryptedData;
				console.log('encryptedData是', encryptedData);
				let iv = e.detail.iv;
				console.log('iv是', iv);
				if (encryptedData != null && iv != null) { //如果不为空，
					console.log('允许按钮点击了');
					var mobileNumber = this.$global.getMobile(encryptedData, iv); //手机号授权成功--获取到手机号
					this.requestData(); //发送请求，获取token
					this.$store.commit('changisLogin'); //此时isLogin:false

					wx.switchTab({ //跳转到首页
						url: '../firstPage/firstPage'
					});

					return mobileNumber; //返回值：手机号
				}
			},
			onClickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			requestData() {
				try {
					var openid = uni.getStorageSync('openid'); //openid的值

					var appid = uni.getStorageSync('appid');

					var shoujihao = uni.getStorageSync('shoujihao');

					var nicheng = uni.getStorageSync('nicheng');

					var touxiang = uni.getStorageSync('touxiang');

				} catch (e) {
					console.log("获取缓存错误" + e)
				};
				// 发送请求，获取数据
				request({
					url: '/api/Sys_User/WeChatAuth',
					method: "POST",
					data: {
						AppId: appid,
						TelPhone: shoujihao,
						OpenId: openid,
						NickName: nicheng,
						HeadImg: touxiang,
					},
					success: (res) => {
						var token = res.data.data.token; //点击允许按钮-手机号授权成功，发送请求，获取到token
						uni.setStorage({ //缓存token
							key: 'token',
							data: token,
						});
					},
					fail: (fl) => {
						uni.showError('服务器链接失败', fl);
					}
				});
			}


		}
	}
</script>

<style>
	.hint {
		font-size: 14px;
	}

	.imgLogo {
		width: 76%;
		height: 84%;
	}

	.content {
		text-align: center;
		overflow: hidden;
		/* display: none; */
	}

	.content span {
		margin-top: 20upx;
	}

	.text-area {
		margin-top: 40upx;
	}

	.nameBox {
		text-align: center;
	}

	.text-area .btn {
		width: 90%;
		height: auto;
		background-color: #04BE02;
		color: #fff;
		border: 0px solid #fff;

	}

	.profilePhoto {
		width: 130upx;
		height: 130upx;
		border-radius: 50%;
		margin: 0 auto;
		margin-top: 150upx;
		text-align: center;
		overflow: hidden;
	}

	.profilePhoto img {
		width: 100%;
		height: 100%;
		vertical-align: middle;
	}

	.zhuanBox {
		text-align: center;
		width: 250px;
		font-size: 14px;
		position: absolute;
		left: 50%;
		bottom: 80px;
		transform: translate(-50%, 0);
	}

	.link {
		color: #007AFF;
	}
</style>
